STEP 15: Let's add 1 to score every time the fish catches some food.
In LOGIC, click and drag Global Variable indented into the collision event. Change the variable name from my_var to score . This lets us change the score in the collision event. From , drag Add Value into the collision event. Change the variable from my_var to score . The score is now changing, but we don't see the scoreboard updating! We'll do that in the next activity.
To navigate the page using the TAB key, first press ESC to exit the code editor.
stage.set_background("underwater")
sprite = codesters.Sprite("fish")
sprite.go_to(-100, -220)
stage.disable_floor()
stage.set_gravity(4)
sprite.set_gravity_off()
def right_key():
sprite.move_right(20)
# add other actions...
stage.event_key("right", right_key)
def left_key():
sprite.move_left(20)
# add other actions...
stage.event_key("left", left_key)
score = 0
#my_display = codesters.Display(my_var, x, y)
score_board = codesters.Display(score, -200, 200)
def interval():
rand_x = random.randint(-250, 250)
# sprite = codesters.Circle(x, y, diameter, "color")
food = codesters.Circle(rand_x, 260, 10, "sienna")
# add any other actions...
stage.event_interval(interval, 2)
def collision(sprite, hit_sprite):
stage.remove_sprite(hit_sprite)
# add any other actions...
sprite.event_collision(collision)
t = codesters.Teacher()
global_text = t.find_text('global')
increments = t.find_text('+=')
try:
tval1a = t.get_indent_at_line(global_text[0][0])
tval1b = global_text[0][1]
except:
tval1a = -1
tval1b = "DNE"
try:
tval2a = t.get_indent_at_line(increments[0][0])
tval2b = increments[0][1].replace(" ", "")
except:
tval2a = -1
tval2b = "DNE"
t1 = TestObjective()
t1.add_success(tval1a == 4 and "score" in tval1b, "Great job!")
t1.add_failure(tval1b == "DNE", "Did you add a Global Variable?")
t1.add_failure("score" not in tval1b, "Did you global score?")
t1.add_failure(tval1a < 4, "Did you indent global score inside the collision event?")
t1.add_failure(tval1a > 4, "Oops! Did you indent global score too far?")
t2 = TestObjective()
t2.add_success(tval2a == 4 and tval2b == "score+=1", "Great job!")
t2.add_failure(tval2b == "DNE", "Did you drag out Add Value inside the collision event?")
t2.add_failure(tval2b != "score+=1", "Did you add 1 to score correctly?")
t2.add_failure(tval2a < 4, "Did you indent score += 1 inside the collision event?")
t2.add_failure(tval2a > 4, "Oops! Did you indent score += 1 too far?")
tester = TestManager()
tester.add_test_list([t1, t2])
tester.run_tests()
tester.display_first_feedback()
Run Code
Activity Submitted!
Submit Work
Next Activity
Stop Running Code
Show Chart
Show Console
Reset Code Editor
Codesters How To (opens in a new tab)